

jQuery(function ($) {
	var user = JSON.parse(localStorage.getItem('user_info')) || {};
	var token = user.token;
	$("#sms_btn").click(function () {
		//匹配手机号
		var reg = /^1[3456789]\d{9}$/;
		var loginCode = $.trim($("#tel").val());
		var textCode = $.trim($('#text2').val()); //手机短信验证码
		//非空验证
		if (loginCode == "" || loginCode == null) {
			$("#tel").focus();
			$(".tel-msg").html("请输入正确的手机号或邮箱地址");
			return;
		} else if (textCode == "" || textCode == null) {
			$(".text-msg").html("请输入验证码");
			return;
		}
		if (reg.test($("#tel").val())) {
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/user/login_sms',
				data: JSON.stringify({ phone: loginCode, code: textCode }),
				contentType: "application/json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				error: function error(_error) {
					//请求失败的回调方法 
					alert('网络加载失败，请稍候再试!');
				},
				success: function success(data) {
					if (data.code == 200) {
						var id = data.data.id;
						var uid = data.data.uid;
						var user_name = data.data.loginName;
						var point = data.data.point;
						var levelId = data.data.levelId;
						var balance = data.data.balance;
						var growth = data.data.growth;
						var data = {
							"id": id,
							"uid": uid,
							"user_name": user_name,
							"point": point,
							'token': data.attributes.token,
							"level_id": levelId,
							"balance": balance,
							"growth": growth
						};
						localStorage.setItem("user_info", JSON.stringify(data));
						var isStoreGoods = JSON.parse(localStorage.getItem('noUsers')) || {};
						if (isStoreGoods.length) {
							var goodsIdArr = [];
							for (var i = 0; i < isStoreGoods.length; i++) {
								goodsIdArr.push(isStoreGoods[i].id);
							};
							var data = {
								"activity_id": 0,
								'user_id': id,
								'goods_ids': goodsIdArr,
								type: 1
							};
							send_ajax('post', '/api/cart/add', data, function callback(result) {
								if (result.code == 200) {
									window.localStorage.removeItem('noUsers');
									var prevLink = document.referrer;
									var url = window.location.host;
									if ($.trim(prevLink) == '') {
										location.href = '/index.html';
									} else {
										if (prevLink.indexOf(url) == -1) {
											//来自其它站点
											location.href = '/';
											return;
										}
										if (prevLink.indexOf('/login') != -1) {
											//来自注册页面
											location.href = '/';
											return;
										}
										location.href = prevLink;
									}
								}
							});
						} else {
							var prevLink = document.referrer;
							var url = window.location.host;
							if ($.trim(prevLink) == '') {
								location.href = '/index.html';
							} else {
								if (prevLink.indexOf(url) == -1) {
									//来自其它站点
									location.href = '/';
									return;
								}
								if (prevLink.indexOf('/login') != -1) {
									//来自注册页面
									location.href = '/';
									return;
								}
								location.href = prevLink;
							}
						};
					} else {
						$(".tel-msg").html(data.msg);
					}
				}
			});
		};
	});
	$(document).keydown(function (event) {
		if (event.keyCode == 13) {
			$('#sms_btn').triggerHandler('click');
		}
	});
});